package io.github.tuuzed.hydrogen.simple;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.List;

import io.github.tuuzed.adapter.Items;
import io.github.tuuzed.adapter.RecyclerViewAdapter;
import io.github.tuuzed.hydrogen.Select;

public class MainActivity extends AppCompatActivity {
    private Items items = new Items();
    private RecyclerViewAdapter adapter = new RecyclerViewAdapter(items);

    @Override
    protected void onRestart() {
        super.onRestart();
        items.clear();
        List<UserModel> users = new Select().from(UserModel.class).all();
        items.addAll(users);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, UserActivity.class);
                startActivity(intent);
            }
        });
        adapter.register(UserModel.class, new UserItemComponent(this));
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(adapter);
        List<UserModel> users = new Select().from(UserModel.class).all();
        items.addAll(users);
        adapter.notifyDataSetChanged();

    }

}
